
#include <iostream>
#include <vector>
using namespace std;

int log(int n)
{
        int ans = 0;
        n >>= 1;
        while(1<<ans <= n){
                ans++;
        }
        return ans;
}

int main()
{
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        
        int m, n;
        cin >> n >> m;
        int k = log(n);
        vector<vector<int>> num(n + 1, vector<int>(k + 1, 0));
        vector<int> logn(n + 1, 0);
        for(int i = 1; i <= n; i++){
                cin >> num[i][0];
        }

        for(int j = 1; j <= k; j++){
                for(int i = 1; i + (1 << j) - 1 <= n; i++){
                        num[i][j] = max(num[i][j - 1], num[i + (1 << (j - 1))][j - 1]);
                }
        }
        
        logn[0] = -1;
        for(int i = 1; i <= n; i++){
                logn[i] = logn[i >> 1] + 1;
        }

        for(int i = 0, l, r; i < m; i++){
                cin >> l >> r;
                int s = logn[r - l + 1];
                cout << max(num[l][s], num[r - (1 << s) + 1][s]) << '\n';
        }
        //cout << endl;
        return 0;
}